/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.distrimas.senta.dominio.compras.proveedores;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Sebastian
 *         Alex
 */
public class GestionarProveedoresController extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private ProveedorDTO proveedor;
    private ProveedorDAO proveedorDao;
    private String validame[]= new String[9];
    private ValidaProveedores vp;

    protected void ingresarProveedor(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            boolean ingreso=true,dvalidos= false,dval;
            proveedor = new ProveedorDTO();
            proveedorDao = ProveedorDAO.getProveedorDAO();
            validame[0]=request.getParameter("tnit");
            validame[1]=request.getParameter("tcontacto");
            validame[2]=request.getParameter("trazons");
            validame[3]=request.getParameter("tmail");
            validame[4]=request.getParameter("ttel");
            validame[5]=request.getParameter("tdivisa");
            validame[6]=request.getParameter("tcreditobase");
            validame[7]=request.getParameter("tdireccion");
            validame[8]=request.getParameter("tcalificacion");
            vp= new ValidaProveedores(validame,9);
            
            dvalidos = vp.getValidacion();

            
            if(dvalidos==true){
            proveedor.setContacto(request.getParameter("tcontacto" ));
            proveedor.setRazonSocial(request.getParameter("trazons"));
            proveedor.setNit(request.getParameter("tnit"));
            proveedor.setMail(request.getParameter("tmail"));
            proveedor.setTelefono(request.getParameter("ttel"));
            proveedor.setDivisa(request.getParameter("tdivisa"));
            proveedor.setCreditoBase(Float.parseFloat(request.getParameter("tcreditobase")));
            proveedor.setCreditoDisponible(Float.parseFloat(request.getParameter("tcreditobase")));
            proveedor.setCalificacion(Float.parseFloat(request.getParameter("tcalificacion")));
            proveedor.setDireccion(request.getParameter("tdireccion"));
            ingreso = proveedorDao.ingresarProveedor(proveedor);
            }else{ingreso=false;}

            if (ingreso==true){
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Ingresar Proveedor</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("<h1> Ingreso el proveedor exitosamente"+"</h1>");
                out.println("</body>");
                out.println("</html>");

            }else{
                out.println("<html>");
                out.println("<head>");
                out.println("<title>ERROR</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Error al ingresar el proveedor"+"</h1>");
                out.println("</body>");
                out.println("</html>");
            }

        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        ingresarProveedor(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        ingresarProveedor(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
